<?php
	include_once "dataservice.php";
	
	class quangcao_db
	{
		public function Them($TenQuangCao, $HinhAnh, $Link, $MaTrangThai, $NgayHetHan, $ViTri, $PIN)
		{
			$sql = "INSERT INTO `tbl_quangcao`(`TenQuangCao`, `HinhAnh`, `Link`, `MaTrangThai`, `NgayHetHan`, `ViTri`, `PIN`) 
					VALUES ('$TenQuangCao', '$HinhAnh', '$Link', '$MaTrangThai', '$NgayHetHan', '$ViTri', '$PIN')";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function CapNhat($MaQuangCao, $TenQuangCao, $HinhAnh, $Link, $MaTrangThai, $NgayHetHan, $ViTri, $PIN)
		{
			$sql = "UPDATE 	`tbl_quangcao`
					SET 	`TenQuangCao` = '$TenQuangCao',
							`HinhAnh`     = '$HinhAnh',
							`Link`        = '$Link',
							`MaTrangThai` = '$MaTrangThai',
							`NgayHetHan`  = '$NgayHetHan',
							`ViTri`       = '$ViTri',
							`PIN`         = '$PIN'
					WHERE 	`MaQuangCao`  = '$MaQuangCao'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function CapNhatTrangThai($MaQuangCao, $MaTrangThai)
		{
			$sql = "UPDATE 	`tbl_quangcao`
					SET 	`MaTrangThai` = '$MaTrangThai'
					WHERE 	`MaQuangCao`  = '$MaQuangCao'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function CapNhatPIN($MaQuangCao, $PIN)
		{
			$sql = "UPDATE 	`tbl_quangcao`
					SET 	`PIN`         = '$PIN'
					WHERE 	`MaQuangCao`  = '$MaQuangCao'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function Xoa($MaQuangCao)
		{
			$sql = "DELETE
					FROM `tbl_quangcao` 
					WHERE `MaQuangCao` = '$MaQuangCao'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function LayDanhSach($MaQuangCao, $TenQuangCao, $HinhAnh, $Link, $MaTrangThai, $NgayHetHan, $ViTri)
		{
			$sql = "SELECT * FROM `tbl_quangcao` WHERE 1 ";
			
			if($MaQuangCao != 0)
				$sql .= " AND MaQuangCao = '$MaQuangCao'";
			if($TenQuangCao != "")
				$sql .= " AND TenQuangCao = '$TenQuangCao'";
			if($HinhAnh != "")
				$sql .= " AND HinhAnh = '$HinhAnh'";
			if($Link != "")
				$sql .= " AND Link = '$Link'";
			if($MaTrangThai != -1)
				$sql .= " AND MaTrangThai = '$MaTrangThai'";
				
			$sql .= " ORDER BY `ViTri` ASC";
			
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
	}
?>